﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections.Concurrent;

namespace UserData
{
    public enum LogType
    {
        Info,
        Warn,
        Err
    }
    public struct LogValue
    {
        public string value;
        public LogType logType;
        public LogValue(string value,LogType logType)
        {
            this.value = value;
            this.logType = logType;
        }
    }
    public partial class RunLoggeHelp : UserControl
    {
        
        public RunLoggeHelp()
        {
            InitializeComponent();
            event_ShowValueDelegate += show;
            var backGroundWorker = new BackgroundWorker();
            backGroundWorker.DoWork += BackGroundWorker_DoWork;
            backGroundWorker.RunWorkerAsync();
        }
        private object lockObject = new object();
        private void BackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (LogValue item in blockingCollection.GetConsumingEnumerable())
            {
                string value = "";
                if (item.logType == LogType.Info) value = $"[{DateTime.Now.ToString("HH:mm:ss")}] Info:{item.value}";
                if (item.logType == LogType.Warn) value = $"[{DateTime.Now.ToString("HH:mm:ss")}] Wran:{item.value}";
                if (item.logType == LogType.Err) value = $"[{DateTime.Now.ToString("HH:mm:ss")}] Err:{item.value}";
                if(!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string fileName = $"{filePath}\\{DateTime.Now.ToString("yyyy-MM-dd")}.txt";
                event_ShowValueDelegate(item.value, item.logType);
                lock (lockObject)
                {
                    try
                    {
                        FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.WriteLine(value);
                        sw.Close();
                        fs.Close();
                            
                    }
                    catch (Exception ex)
                    {

                        
                    }
                }
            }
        }

        private void dg_LogShow_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        BlockingCollection<LogValue> blockingCollection = new BlockingCollection<LogValue>();
        private delegate void ShowValueDelegate(string logValue, LogType type);
        private ShowValueDelegate event_ShowValueDelegate;

        private string filePath = @"C:/设备Log";
        private string stationName = "";
        public void setFilePath(string filePathName)
        {
            this.filePath = filePathName;
        }
        public void Write(string logValue,LogType logType=LogType.Info)
        {
            blockingCollection.Add(new LogValue(logValue, logType));
        }

        public void show(string meg,LogType type=LogType.Info)
        {
            if(InvokeRequired)
            {
                this.BeginInvoke(new Action(() => show(meg, type)));
            }
            else
            {
                Color color = Color.Black;
                switch (type)
                {
                    case LogType.Info:
                        color = Color.Black;
                        break;
                    case LogType.Warn:
                        color = Color.Blue;
                        break;
                    case LogType.Err:
                        color = Color.Red;
                        break;
                }
                meg =$"{DateTime.Now.ToString("HH:mm:ss")} {meg} \r\n";
                richTextBox1.SelectionStart = richTextBox1.TextLength;
                richTextBox1.SelectionLength = 0;
                richTextBox1.SelectionColor = color;
                richTextBox1.AppendText(meg);
                richTextBox1.SelectionColor = richTextBox1.ForeColor;
                richTextBox1.ScrollToCaret();
            }
        }

        public void clear()
        {
            if(InvokeRequired)
            {
                this.BeginInvoke(new Action(() => clear()));
            }
            else
            {
               // dg_LogShow.Rows.Clear();
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
